﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using Kiva7.DataClasses;

namespace Kiva7.ViewModels
{
    public class LoanViewModelCollection : Collection<Loan>
    {
        public LoanViewModelCollection(string title)
        {
            Title = title;
        }

        public string Title { get; set; }

        public double CountAsDouble
        {
            get
            {
                return Count;
            }
        }

        private double _loanAmount;

        public double LoanAmount
        {
            get
            {
                return _loanAmount;
            }
        }

        private double _fundedAmount;

        public double FundedAmount
        {
            get
            {
                return _fundedAmount;
            }
        }

        private double _paidAmount;

        public double PaidAmount
        {
            get
            {
                return _paidAmount;
            }
        }

        private void CalculateAddition(Loan loan)
        {
            _loanAmount += loan.LoanAmountActual;
            _fundedAmount += loan.FundedAmount;
            _paidAmount += loan.PaidAmount;
        }

        private void CalculateSubtraction(Loan loan)
        {
            _loanAmount -= loan.LoanAmountActual;
            _fundedAmount -= loan.FundedAmount;
            _paidAmount -= loan.PaidAmount;
        }

        protected override void InsertItem(int index,
                                           Loan item)
        {
            base.InsertItem(index, item);
            CalculateAddition(item);
        }

        protected override void RemoveItem(int index)
        {
            CalculateSubtraction(this[index]);
            base.RemoveItem(index);
        }

        protected override void SetItem(int index,
                                        Loan item)
        {
            CalculateAddition(item);
            CalculateSubtraction(this[index]);
            base.SetItem(index,item);
        }
    }
}